package com.paireach.bw.broadcastreceiver;

import android.app.Notification;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Handler;
import android.util.Log;

import com.paireach.bw.R;
import com.paireach.bw.application.MyApplication;
import com.paireach.bw.common.Constant;

import java.util.Date;

import es.dmoral.toasty.Toasty;

/**
 * 网络状态判断，当网络断开时进行提醒
 */
public class NetStatueUtil {
    /**
     * 广播接收器
     */
    public MyReceiver receiver;
    boolean isConnection = true;

    private static final String TAG = "NetStatueUtil";
    private static NotificationManager manager;

    /**
     * 初始化广播接收处理
     *
     * @param context
     */

    public void initReceiver(Context context) {
        receiver = new MyReceiver();
        IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
        context.registerReceiver(receiver, filter);
        manager = (NotificationManager) MyApplication.getContext().getSystemService(Context.NOTIFICATION_SERVICE);
    }

    /**
     * 销毁广播
     *
     * @param context
     */
    public void destroyReceiver(Context context) {
        if (receiver != null) {
            context.unregisterReceiver(receiver);
        }
    }

    /**
     * 网络状态改变时接收到的广播，并进行处理
     *
     * @author Administrator
     */
    public class MyReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo mobNetInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
            NetworkInfo wifiNetInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

            if (!mobNetInfo.isConnected() && !wifiNetInfo.isConnected()) {
                //网络连接断开
                Log.e(TAG, "网络断开连接");
                isConnection = false;
                showNotification("网络连接失败，请检查网络");
            } else if (mobNetInfo.isConnected() || wifiNetInfo.isConnected()) {
                try {
                    manager.cancel(1);
                } catch (NullPointerException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    private static void showNotification(String contentText) {
        Notification.Builder builder = new Notification.Builder(MyApplication.getContext());
        builder.setContentTitle("百威-我来运")
                .setContentText(contentText)
                .setLargeIcon(BitmapFactory.decodeResource(MyApplication.getContext().getResources(), R.mipmap.tts_logo_1))
                .setSmallIcon(R.mipmap.logo16)
                .setPriority(Notification.PRIORITY_HIGH)
                .setDefaults(Notification.DEFAULT_ALL)
                .setAutoCancel(true);

        Notification notification = builder.build();
//            notification.flags = Notification.FLAG_INSISTENT;//不点击的话会一直震动
        manager.notify(1, notification);
    }
}
